package config

import (
	"fmt"
	"github.com/spf13/viper"
	"log"
)

// RedisConfig defines the structure for the Redis configuration
type RedisConfig struct {
	Addr     string `mapstructure:"addr"`
	Password string `mapstructure:"password"`
	DB       int    `mapstructure:"db"`
}

// AccountStrategy defines the trading strategy for an account
type AccountStrategy struct {
	Key                 string  `mapstructure:"key"`                   // 策略唯一标识
	OrderAmount         float64 `mapstructure:"order_amount"`          // 下单金额
	TpProfitRatio       float64 `mapstructure:"tp_profit_ratio"`       // 止盈比例
	MaxPositionQuantity float64 `mapstructure:"max_position_quantity"` // 最大持仓数量
	CoverPositionRatio  float64 `mapstructure:"cover_position_ratio"`  // 补仓比例
	TradingCurrency     string  `mapstructure:"trading_currency"`      // 交易币种
	TradingDirection    string  `mapstructure:"trading_direction"`     // 交易方向 long/short
}

// AccountConfig defines the structure for account configuration
type AccountConfig struct {
	Name         string             `mapstructure:"name"`
	Apikey       string             `mapstructure:"apikey"`
	Secret       string             `mapstructure:"secret"`
	Password     string             `mapstructure:"password"`
	StrategyList []*AccountStrategy `mapstructure:"strategy_list"`
}

// Config defines the structure for the overall configuration
type Config struct {
	Redis    RedisConfig      `mapstructure:"redis"`
	Accounts []*AccountConfig `mapstructure:"accounts"`
}

// ReadConfig 读取配置文件
func ReadConfig(path string) (*Config, error) {
	var config = &Config{}
	viper.SetConfigFile(path)

	if err := viper.ReadInConfig(); err != nil {
		log.Fatalf("Error reading config file, %s", err)
		return config, err
	}

	if err := viper.Unmarshal(config); err != nil {
		log.Fatalf("Unable to decode into struct, %s", err)
		return config, err
	}

	fmt.Printf("Config: %+v\n", config)
	return config, nil
}
